package com.yun.zf.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yun.zf.admin.mapper.ZfCustomerAgentMapper;
import com.yun.zf.admin.service.ZfCustomerAgentService;
import com.yun.zf.common.admin.dto.customer.CustomerAgentDto;
import com.yun.zf.common.admin.model.customer.CooperationMo;
import com.yun.zf.common.admin.model.customer.CustomerAgentInfoMo;
import com.yun.zf.common.app.domain.ZfCustomerAgent;
import com.yun.zf.common.mapstruct.admin.CustomerMapper;
import com.yun.zf.system.security.utils.SecurityUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 客户授权业务员表 服务实现类
 * </p>
 *
 * @author cj
 * @since 2021-03-30
 */
@Service
public class ZfCustomerAgentServiceImpl extends ServiceImpl<ZfCustomerAgentMapper, ZfCustomerAgent> implements ZfCustomerAgentService {


    @Override
    public int addCustomerAgent(ZfCustomerAgent customerAgent) {

        return baseMapper.insert(customerAgent);
    }

    /**
     * 验证业务员数据权限
     *
     * @param customerId
     * @param agentNo
     * @param provCode
     * @param cityCode
     * @return
     */
    @Override
    public int checkAuthorize(Long customerId, int agentNo, Long provCode, Long cityCode) {
        QueryWrapper<ZfCustomerAgent> agentQueryWrapper = new QueryWrapper<>();
        agentQueryWrapper.lambda().eq(ZfCustomerAgent::getAgentNo, agentNo)
                .eq(ZfCustomerAgent::getReference, -1)
                .eq(ZfCustomerAgent::getCustomerId, customerId)
                .eq(ZfCustomerAgent::getCityCode, cityCode).eq(ZfCustomerAgent::getProvinceCode, provCode);
        return baseMapper.selectCount(agentQueryWrapper);
    }


    @Override
    public int referCustomers(Long proCode, Long cityCode, Long customerId, int refNo, String refName) {
        ZfCustomerAgent customerAgent = new ZfCustomerAgent();
        customerAgent.setProvinceCode(proCode);
        customerAgent.setCityCode(cityCode);
        customerAgent.setCustomerId(customerId);
        customerAgent.setAgentNo(SecurityUtils.getLoginUser().getAgentNo());
        customerAgent.setAgentName(SecurityUtils.getLoginUser().getAgentName());
        customerAgent.setReference(refNo);
        customerAgent.setRefName(refName);
        customerAgent.setCreateTime(new Date());
        // 初次推荐为已发送待处理状态
        customerAgent.setStatus("3");
        return baseMapper.insert(customerAgent);
    }


    /**
     * 验证是否为找房经纪人,如果没有数据，则直接返回不能推荐
     *
     * @param proCode
     * @param cityCode
     * @param customerId 客户id
     * @param agentNo    业务员工号
     * @return
     */
    @Override
    public int isBroker(Long proCode, Long cityCode, Long customerId, int agentNo) {
        QueryWrapper<ZfCustomerAgent> customerAgentQueryWrapper = new QueryWrapper<>();
        customerAgentQueryWrapper.lambda().eq(ZfCustomerAgent::getReference, -1).eq(ZfCustomerAgent::getAgentNo, agentNo).eq(ZfCustomerAgent::getCustomerId, customerId)
                .eq(ZfCustomerAgent::getCityCode, cityCode).eq(ZfCustomerAgent::getProvinceCode, proCode)
        ;
        return baseMapper.selectCount(customerAgentQueryWrapper);
    }

    /**
     * 获取业务员联系方式信息
     *
     * @param proCode
     * @param cityCode
     * @param customerId
     * @return
     */
    @Override
    public List<CustomerAgentInfoMo> getCustomerInfo(Long proCode, Long cityCode, Long customerId) {
        return baseMapper.getAgentInfo(proCode, cityCode, customerId);
    }

    @Override
    public int acceptOrRefuse(Long caId, int ar, Long proCode, Long cityCode) {
        int agentNo = SecurityUtils.getLoginUser().getAgentNo();
        QueryWrapper<ZfCustomerAgent> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(ZfCustomerAgent::getReference, agentNo).eq(ZfCustomerAgent::getId, caId).eq(ZfCustomerAgent::getCityCode, cityCode).eq(ZfCustomerAgent::getProvinceCode, proCode);
        ZfCustomerAgent agent = baseMapper.selectOne(wrapper);
        if (ar == 1) {
            agent.setStatus("1");
        } else if (ar == 2) {
            agent.setStatus("2");
        }

        return baseMapper.update(agent, wrapper);
    }

    /**
     * 查询我的合作列表
     * @param c
     * @param proCode
     * @param cityCode
     * @return
     */
    @Override
    public Page<CustomerAgentDto> cooperationList(CooperationMo c, Long proCode, Long cityCode) {
        // 查询除维护信息之外的都算合作信息，我发起的合作代表经纪人是我自己，推荐人是别人，我待处理的合作代表推荐人是我，经纪人是别的发起人
        int agentNo = SecurityUtils.getLoginUser().getAgentNo();
        QueryWrapper<ZfCustomerAgent> wrapper = new QueryWrapper<>();
        wrapper.lambda().ne(ZfCustomerAgent::getReference, -1);
        if (!"BX".equals(c.getStatus())) {
            wrapper.lambda().eq(ZfCustomerAgent::getStatus, c.getStatus());
        }
        if ("BX".equals(c.getType())) {
            wrapper.lambda().and(QueryWrapper -> QueryWrapper.eq(ZfCustomerAgent::getAgentNo, agentNo).or().eq(ZfCustomerAgent::getReference, agentNo));

        }
        if ("1".equals(c.getType())) {
            wrapper.lambda().eq(ZfCustomerAgent::getAgentNo, agentNo);
        }
        if ("2".equals(c.getType())) {
            wrapper.lambda().eq(ZfCustomerAgent::getReference, agentNo);
        }
        wrapper.apply("city_code={0}", cityCode).apply("province_code={0}", proCode);
        Page<ZfCustomerAgent> page = new Page<>(c.getCurrent(), c.getPageSize());
        page = baseMapper.selectPage(page, wrapper);
        List<ZfCustomerAgent> list = page.getRecords();
        // 数据转换为前台返回对象
        List<CustomerAgentDto> dto = CustomerMapper.INSTANCE.listToMo(list);
        Page<CustomerAgentDto> dtoPage = new Page<>(c.getCurrent(), c.getPageSize());
        dtoPage.setRecords(dto);
        dtoPage.setTotal(page.getTotal());
        dtoPage.setCurrent(page.getCurrent());
        dtoPage.setPages(page.getPages());
        return dtoPage;

    }
}
